/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.util;

import java.io.PrintWriter;
import java.util.Set;

import redora.api.BusinessRuleViolation;

/**
 * Translate a (set of) business rule violations to JSON.
 * //TODO do i18n
 * Translates the error messages to the users language.
 * @author jaap
 *
 */
public class BusinessRuleJSON {
    /** Copies the BusinessRuleViolation to JSON. */
    public static void copy(BusinessRuleViolation in, PrintWriter out) {
        out.print('{');
        out.print("\"persistable\":");
        out.print('\"');
        out.print(in.getPersistable().getClass().getSimpleName());
        out.print('\"');
        out.print(',');
        out.print("\"id\":");
        out.print(in.getPersistable().getId());
        if (in.getField() != null) {
            out.print(',');
            out.print("\"field\":");
            out.print("\"");
            out.print(in.getField().name());
            out.print('\"');
        }
        out.print(',');
        out.print("\"businessRuleId\":");
        out.print(in.getBusinessRuleId());
        out.print(',');
        out.print("\"action\":");
        out.print("\"");
        out.print(in.getAction().name());
        out.print("\"");
        if (in.getMessageArguments() != null) {
            out.print(',');
            out.print("messages:");
            out.print('[');
            char comma = ' ';
            for (Object o : in.getMessageArguments()) {
                out.print(comma);
                out.print('{');
                out.print("arguments:");
                out.print('\"');
                out.print(o.toString());
                out.print('\"');
                out.print('}');
                comma = ',';
            }
            out.print(']');
        }

        out.print('}');
    }

    /** Copies the BusinessRuleViolation(s) to JSON. */
    public static void copy(Set<BusinessRuleViolation> in,
            PrintWriter out) {
        out.print('[');
        char comma = ' ';
        for (BusinessRuleViolation br : in) {
            out.print(comma);
            copy(br, out);
            comma = ',';
        }
        out.print(']');
    }
}
